package com.probiz.estoresa.customer.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.customer.Wishlist;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.customer.service.WishlistManager;

public class WishlistController extends GenericStoreAdminController<Wishlist> {
	private WishlistManager wishlistManager = null;

	public void setWishlistManager(WishlistManager wishlistManager) {
		this.wishlistManager = wishlistManager;
	}

	@Override
	protected String getEntityName(Wishlist entity) {
		return null;
	}

	@Override
	protected void initController() throws Exception {
		mgr = wishlistManager;
	}

	@Override
	protected void preSave(HttpServletRequest request, Wishlist entity, BindException errors) {

	}

	@Override
	protected SearchCriteria createSearchCriteria(HttpServletRequest request) {
		Integer customerId = null;
		try {
			String cusId = request.getParameter("customerId");
			if (cusId == null)
				cusId = request.getParameter("appuserId");
			customerId = new Integer(cusId);
		} catch (Exception ex) {
			customerId = Integer.valueOf(0);
		}

		SearchCriteria searchCriteria = super.createSearchCriteria(request, Wishlist.FilterHqlName.FILTER_BY_CUSTOMER);
		searchCriteria.addParamValue(customerId);

		return searchCriteria;
	}

	/**
	 * records delete
	 * 
	 * @param request
	 * @param response
	 */
	public ModelAndView multiDelete(HttpServletRequest request, HttpServletResponse response) {
		if (logger.isDebugEnabled()) {
			logger.debug("entering 'WishlistController multiDelete' method...");
		}

		// get pkey array from request
		String[] pkeys = request.getParameterValues("wishListId");
		if (pkeys != null) {
			for (int i = 0; i < pkeys.length; i++) {
				if (pkeys[i] != null && !pkeys[i].equals("")) {
					wishlistManager.deleteById(new Integer(pkeys[i]));
					if (logger.isDebugEnabled()) {
						logger.debug("WishlistController delete key =" + pkeys[i]);
					}
					// save message
					String message = getMessage("wishlist.deleted", new Object[] { new Integer(pkeys[i]) });
					saveMessage(request, message);
				}
			}
		}

		return super.search(request, response);
	}

}
